package com.hjl.servlet; /**
 * @author ServivE
 * @className ${NAME}
 * @description ${description}
 * @version 0.1
 * @date 2024/7/30 10:13
 * @since jdk11
 */

import com.hjl.entity.Admin;
import com.hjl.mapper.AdminMapper;
import com.hjl.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AdminLoginServlet", value = "/login")
public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String aname = request.getParameter("aname");
        String apwd = request.getParameter("apwd");

        Admin admin = new Admin(aname, apwd);

        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        AdminMapper adminMapper = session.getMapper(AdminMapper.class);
        Admin a = adminMapper.adminLogin(admin);


        PrintWriter out = response.getWriter();
        if (a != null) {
            out.println("登陆成功");
        }else {
            response.sendRedirect("adminLogin.html");
        }

    }
}
